﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using EC_Agent.AgentClass;

namespace EC_Agent.Models
{
    public class KhachHangModels
    {
        public DataSet ListKhachHang()
        {
            DataService ds = new DataService();
            return ds.ShowData("ListKhachHang");
        }

        public int InsertKhachHang(KhachHangInfo khachhang)
        {
            SqlParameter[] parameters = new SqlParameter[9];
            parameters[0] = new SqlParameter("@TenKH", khachhang.TenKH);
            parameters[1] = new SqlParameter("@DiaChi", khachhang.DiaChi);
            parameters[2] = new SqlParameter("@Email", khachhang.Email);
            parameters[3] = new SqlParameter("@DienThoai", khachhang.DienThoai);
            parameters[4] = new SqlParameter("@NgaySinh", khachhang.NgaySinh);
            parameters[5] = new SqlParameter("@SoTheTinDung", khachhang.SoTheTinDung);
            parameters[6] = new SqlParameter("@Username", khachhang.Username);
            parameters[7] = new SqlParameter("@Password", khachhang.Password);
            parameters[8] = new SqlParameter("@TinhTrang", khachhang.TinhTrang);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("InsertKhachHang", parameters);
            ds.Disconnect();
            return 1;

        }

        public int UpdateKhachHang(KhachHangInfo khachhang)
        {
            SqlParameter[] parameters = new SqlParameter[8];
            parameters[0] = new SqlParameter("@MaKhachHang", khachhang.MaKhachHang);
            parameters[1] = new SqlParameter("@TenKH", khachhang.TenKH);
            parameters[2] = new SqlParameter("@DiaChi", khachhang.DiaChi);
            parameters[3] = new SqlParameter("@Email", khachhang.Email);
            parameters[4] = new SqlParameter("@DienThoai", khachhang.DienThoai);
            parameters[5] = new SqlParameter("@NgaySinh", khachhang.NgaySinh);
            parameters[6] = new SqlParameter("@SoTheTinDung", khachhang.SoTheTinDung);
            parameters[7] = new SqlParameter("@TinhTrang", khachhang.TinhTrang);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("UpdateKhachHang", parameters);
            ds.Disconnect();
            return 1;

        }

        public int UpdateTTKhachHang(KhachHangInfo khachhang)
        {
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@MaKhachHang", khachhang.MaKhachHang);
            parameters[1] = new SqlParameter("@TinhTrang", khachhang.TinhTrang);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("UpdateTTKhachHang", parameters);
            ds.Disconnect();
            return 1;

        }

        public int UpdateMKKhachHang(KhachHangInfo khachhang)
        {
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@MaKhachHang", khachhang.MaKhachHang);
            parameters[1] = new SqlParameter("@Password", khachhang.Password);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("UpdateMKKhachHang", parameters);
            ds.Disconnect();
            return 1;

        }

        public int DeleteKhachHang(KhachHangInfo khachhang)
        {
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@MaKhachHang", khachhang.MaKhachHang);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("DeleteKhachHang", parameters);
            ds.Disconnect();
            return 1;

        }

        public int CheckLogin(string Username, string Password)
        {
            int error;
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@TenDangNhap", Username);
            parameters[1] = new SqlParameter("@MatKhau", Password);
            DataService ds = new DataService();
            ds.Connect();
            error = ds.ExcuteProcedureWithReturn("spKhachHang_DangNhap", parameters);
            ds.Disconnect();
            return error;
        }

        public int UpdateKH(KhachHangInfo khachhang)
        {
            SqlParameter[] parameters = new SqlParameter[10];
            parameters[0] = new SqlParameter("@MaKhachHang", khachhang.MaKhachHang);
            parameters[1] = new SqlParameter("@TenKH", khachhang.TenKH);
            parameters[2] = new SqlParameter("@DiaChi", khachhang.DiaChi);
            parameters[3] = new SqlParameter("@Email", khachhang.Email);
            parameters[4] = new SqlParameter("@DienThoai", khachhang.DienThoai);
            parameters[5] = new SqlParameter("@NgaySinh", khachhang.NgaySinh);
            parameters[6] = new SqlParameter("@SoTheTinDung", khachhang.SoTheTinDung);
            parameters[7] = new SqlParameter("@Username", khachhang.Username);
            parameters[8] = new SqlParameter("@Password", khachhang.Password);
            parameters[9] = new SqlParameter("@TinhTrang", khachhang.TinhTrang);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("UpdateKH", parameters);
            ds.Disconnect();
            return 1;

        }

        public int UpdatePass(string pass, Int16 MaKH)
        {
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@MaKH", MaKH);
            parameters[1] = new SqlParameter("@MatKhau", pass);
            DataService ds = new DataService();
            ds.Connect();
            ds.ExcuteProcedure("UpdatePass", parameters);
            ds.Disconnect();
            return 1;
        }

        public int CheckUsername(string Username)
        {
            int returnVal;
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@TenDangNhap", Username);
            DataService ds = new DataService();
            ds.Connect();
            returnVal = ds.ExcuteProcedureWithReturn("CheckUsername",parameters);
            ds.Disconnect();
            return returnVal;
        }
    }
}